#cs ----------------------------------------------------------------------------

 AutoIt Version: 3.3.8.1
 Author:         myName

 Script Function:
	Template AutoIt script.

#ce ----------------------------------------------------------------------------
;4,0 17,0 43,0 45,0
;4,0 17,0 43,0 45,0
; Script Start - Add your code below here

#include "monster_proc.au3"

Func parse_input_monster($pos)
	Local $str = input($pos)
	If $str == "" Then
		Return False
	EndIf
	Local $ret = StringRegExp($str,"\b(\d+),(\d+)\b",3)
	If @error = 0 Then
		Local $cur = 0
		Local $ary[100][2]
		For $i = 0 To UBound($ret) - 1 Step 2
			For $j = 0 To $cur - 1
				If $ary[$j][0] = $ret[$i] Then
					Local $old = $ary[$j][1]
					Local $new[UBound($old) + 1]
					For $k = 0 To UBound($old) - 1
						$new[$k] = $old[$k]
					Next
					$new[UBound($old)] = $ret[$i+1]
					$ary[$j][1] = $new
					ContinueLoop 2
				EndIf
			Next
			Local $new[1] = [$ret[$i+1]]
			$ary[$cur][0] = $ret[$i]
			$ary[$cur][1] = $new
			$cur += 1
		Next
		Local $res[$cur][2]
		Local $str = "need: "
		For $i = 0 To $cur - 1

			$str &= " " & $ary[$i][0] & "("
			Local $ii = $ary[$i][1];
			For $j = 0 To UBound($ii) - 1
				$str &= $ii[$j] & " "
			Next
			$str &= ")"

			$res[$i][0] = $ary[$i][0]
			$res[$i][1] = $ary[$i][1]
		Next
		;_DebugOut($str)
		Return $res
	ElseIf @error = 2 Then
		_DebugOut("bad regexp against line2 '" & $str & "'")
	Else
		_DebugOut("bad line '" & $str & "'")
	EndIf
	Return False
EndFunc

Global Const $monster_stage_1 = 10 + 2

Func search_monster($pos)
	Switch $substep[$pos]
		Case 0
			Local $m = monster_ready($pos)
			make_progress($pos)
			_DebugOut("monster: " & $m)
			Switch $m
				Case -1
					StopWork($pos,"simple")
				Case -2
					Return True ;not ready
				Case -3
					Return False ; no monster
				Case -4
					$substep[$pos] = 1 ; always loot need
				Case Else
					Local $need = parse_input_monster($pos)
					$stepdata[$pos] = "test"
					If IsArray($need) Then
						For $i = 0 To UBound($need,1) - 1
							If $m = $need[$i][0] Then
								Local $loot = $need[$i][1]
								If $loot[0] = 0 Then
									StopWork($pos,"yes " & $m)
									ExitLoop
								Else
									$substep[$pos] = 1 ; check loot
									$stepdata[$pos] = $loot
									Return True
								EndIf
							EndIf
						Next
					EndIf
					Return False ; next island
			EndSwitch
		Case 1
			monster_open($pos)
			$substep[$pos] = 2
			next_over($pos,200)
		Case 2 To $monster_stage_1 - 1
			If monster_loot_ready($pos) Then
				$substep[$pos] = $monster_stage_1
			ElseIf $substep[$pos] < $monster_stage_1 - 1 Then
				_DebugOut("loot not ready")
				$substep[$pos] += 1
				next_over($pos,50)
			Else
				StopWork($pos,"cannot open loot")
			EndIf
		Case $monster_stage_1
			If monster_loot_has($pos,$stepdata[$pos]) Then
				StopWork($pos,"monster has " & $stepdata[$pos])
			Else
				$substep[$pos] = $monster_stage_1 + 1
			EndIf
		Case $monster_stage_1 + 1
			monster_close($pos)
			$substep[$pos] = $monster_stage_1 + 2
			next_over($pos,200)
		Case $monster_stage_1 + 2
			Return False
	EndSwitch
	Return True ; need continue work
EndFunc
